//
//  ZHTableViewHelper.m
//  ArthurRoom
//
//  Created by 赵宏亚 on 2019/8/17.
//  Copyright © 2019 ArthurRoom. All rights reserved.
//

#import "ZHTableViewHelper.h"

@interface ZHTableViewHelper ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSString * cellIdentifier;
@end

@implementation ZHTableViewHelper

+ (instancetype)bindingForTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source didSelectionCommand:(RACCommand *)didSelection templateCellClassName:(NSString *)classCell {
    return [[self alloc] initWithTableView:tableView sourceSignal:source didSelectionCommand:didSelection templateCellClassName:classCell];
}

+ (instancetype)bindingForTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source didSelectionCommand: didSelection templateCellNibName:(nonnull NSString *)nibName {
    return [[self alloc] initWithTableView:tableView sourceSignal:source didSelectionCommand:didSelection templateCellNibName:nibName];
}

- (instancetype)initWithTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source didSelectionCommand:(RACCommand *)didSelection templateCellNibName:(nonnull NSString *)nibName {
    if (self = [self initWithTableView:tableView sourceSignal:source didSelectionCommand:didSelection]) {
                
        UINib *templateCellNib = [UINib nibWithNibName:nibName bundle:nil];
        _templateCell = [[templateCellNib instantiateWithOwner:nil options:nil] firstObject];
        self.cellIdentifier = nibName;
        [_tableView registerNib:templateCellNib forCellReuseIdentifier:_cellIdentifier];
        _tableView.rowHeight = _templateCell.bounds.size.height;
        
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
        
        [self customInitialization];
    }
    return self;
}

- (instancetype)initWithTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source didSelectionCommand:(RACCommand *)didSelection templateCellClassName:(NSString *)classCell {
    
    
    if (self = [self initWithTableView:tableView sourceSignal:source didSelectionCommand:didSelection]) {
        self.cellIdentifier = classCell;
        [tableView registerClass:NSClassFromString(classCell) forCellReuseIdentifier:classCell];
        
        [self customInitialization];
    }
    return self;
}


- (instancetype)initWithTableView:(UITableView *)tableView sourceSignal:(RACSignal *)source didSelectionCommand:(RACCommand *)didSelection {
    NSParameterAssert(tableView);
    NSParameterAssert(source);
    self = [super init];
    if (self) {
        _tableView = tableView;
        _data = [NSMutableArray array];
        _selectCommand = didSelection;
        
        @weakify(self)
        [source subscribeNext:^(NSArray *dataList) {
            @strongify(self)
            self.data = [NSMutableArray arrayWithArray:dataList];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                // UI更新代码
                [self.tableView reloadData];
            });
        }];
        
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return self;
}

- (void)customInitialization {
    //abstract...
}

#pragma mark - UITableViewDataSource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _data.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self dequeueCellAndBindInTable:tableView indexPath:indexPath];
}

- (UITableViewCell *)dequeueCellAndBindInTable:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath {
    id<ZHCellDelegate> cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    [cell cellWithModel:_data[indexPath.row]];
    return (UITableViewCell *)cell;
}

#pragma mark - UITableViewDelegate methods
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_selectCommand) {
        [_selectCommand execute:_data[indexPath.row]];
    }
    //点击以后直接恢复正常状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

#pragma mark - Action
- (void)reloadDataWithSourceList:(NSArray *)source {
    if (source) {
        _data = [NSMutableArray arrayWithArray:source];
    }
    [_tableView reloadData];
}



@end
